﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp0
{
    public partial class shang_ren : Form
    {
        public shang_ren()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void shang_ren_Load(object sender, EventArgs e)
        {
            Table_ren();
        }
        private void Table_ren()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Customer";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d;
                a = dr["Cid"].ToString();
                b = dr["Cname"].ToString();
                c = dr["Cphone"].ToString();
                d = dr["Cremark"].ToString();

                string[] str = { a, b, c, d };
                dataGridView1.Rows.Add(str);

            }
            dr.Close();

        }
        private void button2_Click(object sender, EventArgs e)
        {
            
          }

        private void button3_Click(object sender, EventArgs e)
        {//重置密码

            DialogResult dr = MessageBox.Show("确认重置" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                string sql = $"update Customer set Cpassword='123' where Cid='{ dataGridView1.SelectedRows[0].Cells[0].Value.ToString()}'";
                Dao dao = new Dao();
                if (dao.Execute(sql) > 0)
                {
                    MessageBox.Show("成功");
                    Table_ren();//删除刷新
                }
                else
                {
                    MessageBox.Show("失败");
                }

            }
            Table_ren();
        }

        private void button1_Click(object sender, EventArgs e)
        {//注册客户账号
         
            if (textBox3.Text == "")
            {
                MessageBox.Show("输入为空, 请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else
            {
                try
                {
                    Dao dao = new Dao();
                    string sql = $"insert into Customer values('{textBox3.Text}',null,null,null,'123')";
                    int n = dao.Execute(sql);
                    if (n > 0)
                    {
                        MessageBox.Show("成功");

                    }
                }
                catch
                {
                    MessageBox.Show("失败");
                }

            }
            Table_ren();
        }

        private void button4_Click(object sender, EventArgs e)
        {//按编号查询
            Cid_search();
        }
        private void Cid_search()
        {
            dataGridView1.Rows.Clear();
            string sql = $"select * from Customer where Cid='{textBox1.Text}'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c;
                a = dr["Cid"].ToString();
                b = dr["Cname"].ToString();
                c = dr["Cphone"].ToString();
                string[] str = { a, b, c };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();
        }

        private void Cname_search()
        {//查名 显示
            dataGridView1.Rows.Clear();
            string sql = $"select * from Customer where Cname like'%{textBox2.Text}%'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c;
                a = dr["Cid"].ToString();
                b = dr["Cname"].ToString();
                c = dr["Cphone"].ToString();
                string[] str = { a, b, c };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {//按客户名字查询
            Cname_search();
        }

        private void button6_Click(object sender, EventArgs e)
        {//显示全部客户
            Table_ren();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private void dataGridView1_Click(object sender, EventArgs e)
        {//显示 当前选中
            
        }
        private void button2_Click_1(object sender, EventArgs e)
        {

        }
    }
}
